package com.net.wanjian.networkhospitalmanager.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.joooonho.SelectableRoundedImageView;
import com.net.wanjian.networkhospitalmanager.R;
import com.net.wanjian.networkhospitalmanager.activity.skilltrain.LaboratoryListActivity;
import com.net.wanjian.networkhospitalmanager.activity.skilltrain.StudentHistoryLaboratoryListActivity;
import com.net.wanjian.networkhospitalmanager.adapter.base.RecyclerBaseAdapter;
import com.net.wanjian.networkhospitalmanager.bean.StudentHistoryLabReserveResult;
import com.net.wanjian.networkhospitalmanager.consts.LabReserveBtnConst;
import com.net.wanjian.networkhospitalmanager.utils.TimeDateUtils;
import com.net.wanjian.networkhospitalmanager.utils.URLDecoderUtil;
import com.zhy.autolayout.utils.AutoUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Description:
 * Created by lidongcai on 17/6/2.
 * Email:  lidongcaiocc@163.com
 */

public class StudentHistoryReserveAdapter extends RecyclerBaseAdapter<StudentHistoryLabReserveResult.DataBean.PastLabReserveListBean,StudentHistoryReserveAdapter.ViewHolder> {



    public StudentHistoryReserveAdapter(Context context) {
        super(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_my_reserve_list, parent, false);
        AutoUtils.auto(view);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final StudentHistoryLabReserveResult.DataBean.PastLabReserveListBean d, int position) {
//设置实验名称
        holder.setLabNameTv(URLDecoderUtil.getDecoder(d.getLabReserveName()));

        //是否存在指导教师（0不存在，1存在）
        if (URLDecoderUtil.getDecoder(d.getIsExistGuideTeacher()).equals("0")) {
            holder.setLabIsGuideTeacherIv(false);
        } else {
            holder.setLabIsGuideTeacherIv(true);
        }
        //技能名称集合（中间以英文逗号隔开）
        holder.setLabSkillTv("练习技能：" + URLDecoderUtil.getDecoder(d.getTrainTypeNameArray()));

        //设置开放到关闭的时间，需要格式化
        String openTime = TimeDateUtils.formatTimeFriend(URLDecoderUtil.getDecoder(d.getLabReserveOpenTime()));
        String closeTime = TimeDateUtils.formatTimeFriend(URLDecoderUtil.getDecoder(d.getLabReserveCloseTime()));
        String ot = openTime.split(" ")[0];
        String ct = closeTime.split(" ")[0];
        if (ot.equals(ct)) {
            holder.setLabStartTimeTv(openTime + "—" + closeTime.split(" ")[1]);
        } else {
            holder.setLabStartTimeTv(openTime + "—" + closeTime);
        }

        holder.setLabReserveBtn();

        holder.labReserveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString(StudentHistoryLaboratoryListActivity.LAB_RESERVE_MEMBER_ID_KEY, URLDecoderUtil.getDecoder(d.getLabReserveMemberID()));
                bundle.putString(StudentHistoryLaboratoryListActivity.LAB_RESERVE_NAME_KEY, URLDecoderUtil.getDecoder(d.getLabReserveName()));
                bundle.putString(StudentHistoryLaboratoryListActivity.LAB_RESERVE_ID_KEY, URLDecoderUtil.getDecoder(d.getLabReserveID()));
                Intent intent = new Intent(context, StudentHistoryLaboratoryListActivity.class);
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });

    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.lab_name_tv)
        TextView labNameTv;
        @BindView(R.id.lab_skill_tv)
        TextView labSkillTv;
        @BindView(R.id.lab_start_time_tv)
        TextView labStartTimeTv;
        @BindView(R.id.lab_is_guide_teacher_iv)
        SelectableRoundedImageView labIsGuideTeacherIv;
        @BindView(R.id.relativeLayout)
        RelativeLayout relativeLayout;
        @BindView(R.id.lab_reserve_btn)
        Button labReserveBtn;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }


        public void setLabNameTv(String labNameTv) {
            this.labNameTv.setText(labNameTv);
        }

        public void setLabSkillTv(String labSkillTv) {
            this.labSkillTv.setText(labSkillTv);
        }

        public void setLabStartTimeTv(String labStartTimeTv) {
            this.labStartTimeTv.setText(labStartTimeTv);
        }

        public void setLabIsGuideTeacherIv(boolean b) {
            if (b) {
                labIsGuideTeacherIv.setBackgroundResource(R.mipmap.practice_btn_green);
            } else {
                labIsGuideTeacherIv.setBackgroundResource(R.mipmap.practice_btn_grey);
            }
        }


        public void setLabReserveBtn() {
            labReserveBtn.setEnabled(true);
            labReserveBtn.setText("查 看");
            labReserveBtn.setBackgroundResource(R.drawable.btn_bg_shape1);
        }
    }
}
